package com.cpd.blog.common.utils;

import com.cpd.blog.dto.User.UserVo;


/**
 * @author Copydown
 * @date 2021/8/17 16:05
 */
public class CurrentUser {

    private CurrentUser() {
    }

    /**
     * ThreadLocal用于线程变量隔离，存储每个线程特有的信息
     */
    private static final ThreadLocal<UserVo> LOCAL = new ThreadLocal<>();

    public static void put(UserVo userVo) {
        LOCAL.set(userVo);
    }

    public static ThreadLocal<UserVo> getLocalMap() {
        return LOCAL;
    }

    public static UserVo get() {
        return LOCAL.get();
    }

    public static String getEmail() {
        return LOCAL.get().getEmail();
    }

    public static void remove() {
        LOCAL.remove();
    }

    public static Long getId() {
        return Long.parseLong(LOCAL.get().getId());
    }

    public static boolean equalsId(Long userId) {
        return CheckUtil.isEquals(LOCAL.get().getId(), userId);
    }

    public static boolean noEqualsId(Long userId) {
        return CheckUtil.isNotEquals(Long.parseLong(LOCAL.get().getId()), userId);
    }

    public static boolean isNull() {
        return CheckUtil.isNull(LOCAL.get());
    }

}
